<template>
  <el-container class="main_con">
    <div class="filter_con ds_block">
      <div class="button_area"><el-button v-has size="medium" type="primary" @click="drawer = true">新增分类</el-button></div>
    </div>
    <div class="list_con">
      <el-table :data="list" element-loading-text fit highlight-current-row>
        <el-table-column prop="name" label="分类名称"></el-table-column>
        <el-table-column prop="img" label="分类图片">
          <div slot-scope="scope" ><vimage style="width: 100px" :src="scope.row.img" ></vimage></div>
        </el-table-column>
        <!-- <el-table-column prop="status" label="状态"></el-table-column> -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-has type="text" @click="EditHandle(scope.row)">编辑</el-button>
            <el-button v-has type="text" @click="handleClose(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--      <div class="table_footer">
        <el-pagination
          @current-change="ChangeCurrent"
          class="pages"
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="size"
          :page-count="page"
          hide-on-single-page
        ></el-pagination>
      </div> -->
    </div>
    <el-dialog title="新增分类" :visible.sync="drawer" destroy-on-close width="500px">
      <el-form label-width="100px">
        <el-form-item label="分类名称"><el-input style="width: 200px;" v-model="from.name"></el-input></el-form-item>
        <el-form-item label="分类图片">
          <div style="width: 100px;height: 100px;"><Upload :tempUrl="from.img" @input="getImage"></Upload></div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="drawer = false">取 消</el-button>
        <el-button type="primary" @click="addHandle">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改分类" :visible.sync="editdrawer" destroy-on-close width="500px">
      <el-form label-width="100px">
        <el-form-item label="分类名称"><el-input style="width: 200px;" v-model="from.name"></el-input></el-form-item>
        <el-form-item label="分类图片">
          <div style="width: 100px;height: 100px;"><Upload :tempUrl="from.img" @input="getImage"></Upload></div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="drawer = false">取 消</el-button>
        <el-button type="primary" @click="edititem">确 定</el-button>
      </div>
    </el-dialog>
  </el-container>
</template>

<script>
import {getclassifylist, addclassify, editclassify, delclassify} from '@/api/mall/integral';
import Upload from '@/components/Upload/SingleImage2';
export default {
  data() {
    return {
      list: [],
      from: {
        name: '',
        img: ''
      },
      drawer: false,
      editdrawer: false
    };
  },
  mounted() {
    this.getlist();
  },
  components: {
    Upload
  },
  methods: {
    getlist() {
      let data = {};
      getclassifylist(data).then(res => {
        this.list = res.msg;
      });
    },
    addHandle() {
      addclassify(this.from).then(res => {
        this.drawer = false;
        this.from = {
          name: '',
          img: ''
        };
        this.getlist();
      });
    },
    getImage(val) {
      this.from.img = val;
    },
    EditHandle(item) {
      this.from = {
        name: item.name,
        img: item.img,
        id: item.id
      };
      this.editdrawer = true;
    },
    edititem() {
      editclassify(this.from).then(res => {
        this.from = {
          name: '',
          img: ''
        };
        this.getlist();
        this.editdrawer = false;
      });
    },
    handleClose(id) {
      let _this = this;
      this.$confirm('确认删除？')
        .then(_ => {
          let data = {
            id: id
          };
          delclassify(data).then(res => {
            _this.getlist();
          });
        })
        .catch(_ => {});
    }
  }
};
</script>

<style></style>
